//
//  AppDelegate.m
//  bestkeep
//
//  Created by 毛韶谦 on 2016/11/3.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "AppDelegate.h"
#import "RootTabBarController.h"
#import "KeyboardManager.h"
#import "PushManager.h"
#import "ShareManager.h"
#import "Mediator+BKMy.h"
#import "Mediator+BKOrder.h"
#import "BKGDManagerViewController.h"
#import "BKVersionCheckMannager.h"
#import "BK3DTouchFuction.h"
#import "BKWidigitMannager.h"
#import "Mediator+BKGuidePage.h"
#import "LocalPushManager.h"
#import "UIView+ParentController.h"
#import "BK_StorePay_VC.h"
@interface AppDelegate ()

@end

/**
 AppDelegate 类中的代码不允许存在除初始化UI以外的代码 或者 自定义Property，只能有调用代码。（如 支付回调、分享回调、推送等请在其他类中完成，只在此处调用）
 */
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [Growing startWithAccountId:@"892b15ab7142af61"];
    [RequestManager manager];
    NSInteger enviromentValue = ApplicationEnvironmentProduction ? 2 : 3;
    [[Mediator sharedInstance] setDevelopEnviromentWithValue:enviromentValue];
    
    [self initUI];
    [[PushManager manager] registerUserNotification:application didFinishLaunchingWithOptions:launchOptions];
    [ShareManager manager];
    [self keyboardManagerConfig];
    
    
    [[Mediator sharedInstance]registerEaseMobileWithApplication:application easeMobKey:BestKeepEaseMobServericeKey launchOptions:launchOptions];
    
    [[Mediator sharedInstance]getMessageStatusWithApplication:application didFinishLaunchingWithOptions:launchOptions];
   // [[Mediator sharedInstance]getUnreadMessageNum];
    
    [BK3DTouchFuction creat3DTouchFunction];
    
    return YES;
}

/**
 *  初始化界面
 */
-(void)initUI {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    RootTabBarController *tabBarController = [[RootTabBarController alloc] init];
    self.window.rootViewController = tabBarController;
    
    [self.window makeKeyAndVisible];
    [[Mediator sharedInstance]BKGuidePageChoiceAppear];
     
}

-(void)keyboardManagerConfig {
    
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
    keyboardManager.enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
}

#pragma mark -- 3DTouchFuction
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    BK3DTouchFuction * touchFunction = [[BK3DTouchFuction alloc]init];
    [touchFunction acessOf3DTouchWithApplication:application performActionForShortcutItem:shortcutItem window:self.window];
}

#pragma -mark Application

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[PushManager manager] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [[PushManager manager] application:application didFailToRegisterForRemoteNotificationsWithError:error];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [[PushManager manager] application:application didReceiveRemoteNotification:userInfo];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    NSLog(@"applicationWillResignActive");
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSLog(@"applicationDidEnterBackground");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [[Mediator sharedInstance]getMessageWhenApplicationWillEnterForeground:application];
    NSLog(@"applicationWillEnterForeground");
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [[BKVersionCheckMannager shareInstance] checkBKVersion];
    [[Mediator sharedInstance]getMessgaeWhthApplicationDidBecomeActive:application];
    if ([[UIView BKcurrentViewController] isKindOfClass:[BK_StorePay_VC class]]) {
        
        BK_StorePay_VC * vc =(BK_StorePay_VC *)[UIView BKcurrentViewController];
        [vc appDidBecomeActive_RelayOutQR];
    }
    NSLog(@"applicationDidBecomeActive");
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    
    [[Mediator sharedInstance]getMessageNumWhenApplication:application didReceiveRemoteNotification:userInfo window:self.window];
}



-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    [LocalPushManager application:application didReceiveLocalNotification:notification];
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
         [[Mediator sharedInstance] BKOrder_AliHandleOpenUrl:url];
    }
    if ([[url scheme] isEqualToString:@"wx2c7367321c4ecab5"]){
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }
    
    if ([[url scheme] isEqualToString:@"medicalWidgets"]) {
        
        [self performSelector:@selector(inputFormWidgeWithIndex:) withObject:[url host] afterDelay:0.3f];
        return YES;
    }
    
//    if([Growing handleUrl:url]) {
//        return YES;
//    }
    if ([[url scheme] isEqualToString:@"bestkeep"]) {
        
        NSLog(@"浏览器跳转BESTKEEP");
    }
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
    });
    return result;
    
}

+ (void)initialize {
    
}

-(void)inputFormWidgeWithIndex:(NSString *)index
{
   
    [BKWidigitMannager clickBtnAtIndex:index window:self.window];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    if ([[url scheme] isEqualToString:@"wx2c7367321c4ecab5"]){
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }
    return result;
}


@end
